﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;
using System.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_ContatReport : CommonPageBase
    {
        public string t = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(GetWhere());

                decimal d = TbsDbSession.Default.From<Proj_Contat>().Select(Proj_Contat._.AMOUNT.Sum()).ToScalar<decimal>();
                decimal p = TbsDbSession.Default.FromSql("select sum(PAMOUT) from Proj_Rement a,Proj_Contat b where a.CONNUB=b.CONNUB").ToScalar<decimal>(); //TbsDbSession.Default.From<Proj_Popayf>().Select(Proj_Popayf._.REMOUT.Sum()).ToScalar<decimal>();

                ToolbarText2.Text = "<b>合同未收金额:<font color='red'>" + (d - p).ToString() + "</font></b>";


                OutputSummaryData();
            }
        }

        private void OutputSummaryData()
        {
            WhereClip wc = WhereClip.All;
            string sql = "";
            if (!string.IsNullOrEmpty(Request["cno"]))
            {
                sql += " and b.cutnub like '%" + Request["cno"] + "%'";
                wc = wc && Proj_Contat._.CUTNUB.Contain(Request["cno"]);
            }
            if (!string.IsNullOrEmpty(Request["stime"]))
            {
                sql += " and b.addtime>'" + Request["stime"] + "'";
                wc = wc && Proj_Contat._.AddTime > Convert.ToDateTime(Request["stime"]);
            }
            if (!string.IsNullOrEmpty(Request["etime"]))
            {
                sql += " and b.addtime<'" + Request["etime"] + "'";
                wc = wc && Proj_Contat._.AddTime < Convert.ToDateTime(Request["etime"]);
            }

            decimal d = TbsDbSession.Default.From<Proj_Contat>().Select(Proj_Contat._.AMOUNT.Sum()).Where(wc).ToScalar<decimal>();
            decimal p = TbsDbSession.Default.FromSql("select sum(PAMOUT) from Proj_Rement a,Proj_Contat b where a.CONNUB=b.CONNUB " + sql).ToScalar<decimal>();



            float donateTotal = 0.0f;
            float feeTotal = 0.0f;
            //foreach (DataRow row in source.Rows)
            //{
            //    donateTotal += Convert.ToInt32(row["Donate"]);
            //    feeTotal += Convert.ToInt32(row["Fee"]);
            //}
            donateTotal = float.Parse(d.ToString());
            feeTotal = float.Parse((d - p).ToString());
            JObject jo = new JObject();
            jo.Add("donateTotal", donateTotal);
            jo.Add("feeTotal", feeTotal);

            t = jo.ToString(Newtonsoft.Json.Formatting.None);

        }

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Proj_Contat>().Where(wc).Count();

            List<Proj_Contat> items = TbsDbSession.Default.From<Proj_Contat>().Where(wc).OrderBy(Proj_Contat._.ID.Desc).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;

            if (!string.IsNullOrEmpty(Request["cno"]))
            {

                wc = wc && Proj_Contat._.CUTNUB.Contain(Request["cno"]);
            }
            if (!string.IsNullOrEmpty(Request["stime"]))
            {
                wc = wc && Proj_Contat._.AddTime > Convert.ToDateTime(Request["stime"]);
            }
            if (!string.IsNullOrEmpty(Request["etime"]))
            {
                wc = wc && Proj_Contat._.AddTime < Convert.ToDateTime(Request["etime"]);
            }
            return wc;
        }
        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string id = e.Values[6].ToString();
            decimal dc = 0;
            decimal z = decimal.Parse(e.Values[5].ToString());
            string conno = e.Values[0].ToString();
            List<Proj_Rement> rementList = TbsDbSession.Default.From<Proj_Rement>().Where(Proj_Rement._.CONNUB == conno).ToList();
            foreach (Proj_Rement item in rementList)
            {
                dc += item.PAMOUT.Value;
            }
            decimal y = z - dc;
            e.Values[6] = "<a href=\"javascript:;\" onclick=\"TabView('/Project/Pro_RementList.aspx?cid=" + id + "', 'tab_wxaddasdf', '查看收款记录', '');\">" + y.ToString() + "</a>";
        }
    }
}